﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Elasticsearch.Net;
using Nest;

namespace ElasticSearchDemo.Service
{
    /// <summary>
    /// 访问ElasticSearch服务接口类
    /// </summary>
    public interface IESServer
    {
        /// <summary>
        /// Linq查询的官方Client
        /// </summary>
        IElasticClient ElasticLinqClient { get; set; }

        /// <summary>
        /// Js查询的官方Client
        /// </summary>
        IElasticLowLevelClient ElasticJsonClient { get; set; }


        /// <summary>
        /// Add Document
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <param name="indexName"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<CreateResponse> AddAsync<TEntity>(TEntity entity, string indexName, string id) where TEntity : class;


        /// <summary>
        /// Add Range Doucment
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <param name="indexName"></param>
        /// <returns></returns>
        Task<BulkResponse> AddRangeAsync<TEntity>(IEnumerable<TEntity> entity, string indexName) where TEntity : class;



        /// <summary>
        /// Update Document
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <param name="indexName"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<UpdateResponse<TEntity>> UpdateAsync<TEntity>(TEntity entity, string indexName, string id) where TEntity : class;


        /// <summary>
        /// Delete Document
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="entity"></param>
        /// <param name="indexName"></param>
        /// <returns></returns>
        Task<DeleteResponse> DeleteAsync(string indexName, string id);


        /// <summary>
        /// Get Document
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="indexName"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        Task<GetResponse<TEntity>> GetAsync<TEntity>(string indexName, string id) where TEntity : class;

        /// <summary>
        /// 判断索引是否存在
        /// </summary>
        /// <param name="indexName"></param>
        /// <param name="selector"></param>
        /// <returns></returns>
        Task<bool> ExistsIndex(string indexName, Func<IndexExistsDescriptor, IIndexExistsRequest> selector = null);


        /// <summary>
        /// 创建索引
        /// </summary>
        /// <param name="indexName"></param>
        /// <param name="numberOfShards"></param>
        /// <param name="numberOfReplicas"></param>
        /// <returns></returns>
        Task<CreateIndexResponse> CreateIndex(string indexName, int numberOfShards = 5, int numberOfReplicas = 1);



        /// <summary>
        /// 删除索引
        /// </summary>
        /// <param name="elasticClient"></param>
        /// <param name="indexName"></param>
        Task<DeleteIndexResponse> DeleteIndexAsync(string indexName);


        /// <summary>
        /// 根据SQL获取数据
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        DataTable GetDataBySql(string sql);



    }
}
